package Hot100;

import java.util.HashMap;
import java.util.Map;

public class longestConsecutive {
    public static void main(String[] args) {
        int[] nums = {0,3,7,2,5,8,4,6,0,1};
        System.out.println(longestConsecutive(nums));
    }

    public static int longestConsecutive(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        int longest = 0;

        //使用Hash表存储数据
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],i);
        }
        for (int i = 0; i < nums.length; i++) {
            int num = 1;
            int count = 1;
            if(!map.containsKey(nums[i] - 1)){
                while(map.containsKey(nums[i] + num)){
                    count ++;
                    num++;
                }
                if(count > longest)
                    longest = count;
            }
        }
        return longest;
    }
}
